package com.qsd.StudentSystem.service.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qsd.StudentSystem.dao.StudentDao;
import com.qsd.common.po.Student;
import com.qsd.StudentSystem.service.StudentService;
import com.qsd.common.enums.ResultStatus;

@Service
public class StudentServiceImpl implements StudentService {
	
	@Resource
	private StudentDao studentDao;

	@Override
	public ResultStatus login(String username, String password, HttpServletRequest request) {
		Student student = studentDao.queryStudent(username, password);
		if (student != null) {
			request.getSession().setAttribute("student", student);
			return ResultStatus.SUCCESS;
		}
		return ResultStatus.PASSWORD_ERROR;
	}

	@Override
	@Transactional
	public ResultStatus register(Student student, HttpServletRequest request) {
		//注册
		int result = studentDao.saveStudent(student);
		if (result > 0) {
			//登录
			return login(student.getUsername(), student.getPassword(), request);
		}
		return ResultStatus.REGISTER_ERROR;
	}

}
